/**
 * 双指针
 * @param {string} s
 * @return {number}
 */
var countSubstrings = function(s) {
    let res = 0
    for(let i = 0;i<s.length;i++){
        res+=findSubStr(s,i,i)
        res+=findSubStr(s,i,i+1)
    }
    return res
};

function findSubStr(s,l,r){
    let res = 0
    while(s[l] === s[r] && l>=0&& r<s.length){
        l--;
        r++;
        res++
    }
    return res
}



/**
 * 动态规划，i从下往上循环，因为依赖的是i+1
 * @param {string} s
 * @return {number}
 */
var countSubstrings = function (s) {
    let len = s.length,
        dp = new Array(len + 1).fill(0).map(() => new Array(len + 1).fill(false)),
        count = 0;

    for (let i = len; i > 0; i--) {
        for (let j = i; j <= len; j++) {
            if (s[i - 1] === s[j - 1] && (j - i <= 1 || dp[i + 1][j - 1])) {
                count++
                dp[i][j] = true
            }
        }
    }

    return count
};
